package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.management.relation.RelationSupport;
import java.util.List;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.Map;

/**
 * 角色信息管理
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> list = roleService.findAll();
            return new Result(true, "角色查询成功", list);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色查询失败");
        }


    }

    @RequestMapping("/findroleIdsByUserId")
    public Result findroleIdsByUserId(Integer userId) {
        try {
            List<Integer> list = roleService.findroleIdsByUserId(userId);
            return new Result(true, "查找角色ID成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查找角色ID失败");

        }


    }



    /**
     * 根据条件分页查看角色信息
     */
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        System.out.println(queryPageBean);
        PageResult pageResult = roleService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 获取所有的权限和关联的菜单信息
     *
     * @return
     */
    @RequestMapping("/findPermissionAndMenuAll")
    public Result findPermissionAndMenuAll() {
        try {
            Map map = roleService.findPermissionAndMenuAll();
            return new Result(true, MessageConstant.GET_PERMISSION_AND_MENU_LIST_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_PERMISSION_AND_MENU_LIST_FAIL);
        }
    }

    /**
     * 添加角色
     */
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result add(@RequestBody Role role, Integer[] menuIds, Integer[] permissionIds) {

        try {
            roleService.add(role, menuIds, permissionIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    /**
     * 删除功能
     */
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result delete(Integer id) {
        Result result = null;
        try {
            result = roleService.delete(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    /**
     * 根据id查看角色基本信息
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        Role role = roleService.findById(id);
        if (role != null) {
            return new Result(true, MessageConstant.GET_ROLE_SUCCESS, role);
        } else {
            return new Result(false, MessageConstant.GET_ROLE_FAIL);
        }
    }

    /**
     * 根绝角色id查看关联的权限id和菜单id
     */
    @RequestMapping("/findPermissionIdsAndMenuIdsByRoleID")
    public Result findPermissionIdsAndMenuIdsByRoleID(Integer id) {

        Map map = roleService.findPermissionIdsAndMenuIdsByRoleID(id);
        if (map != null) {
            return new Result(true, MessageConstant.GET_PERMISSION_AND_MENU_LIST_SUCCESS, map);
        }
        return new Result(false, MessageConstant.GET_PERMISSION_AND_MENU_LIST_FAIL);

    }

    /**
     * 修改角色信息
     */
    @RequestMapping("/update")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result update(@RequestBody Role role, Integer[] menuIds, Integer[] permissionIds) {

        try {
            roleService.update(role, menuIds, permissionIds);
            return new Result(true, MessageConstant.UPDATE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_ROLE_FAIL);
        }
    }

}
